package com.scm.service;


import com.scm.pojo.*;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TransactionManager
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/7 17:49
 * @Version 1.0
 **/


@Component
@Transactional( rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class FactoryService {

    @Autowired
    private ScmService scmService;

    @Autowired
    private UserModelService userModelService;

    @Autowired
    private PoMainService poMainService;


    @Autowired
    private VenderService venderService;

    @Autowired
    private ProductService productService;


    @Autowired
    private PoItemService poItemService;

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private Util util;

    @Autowired
    private SoMainService soMainService;

    @Autowired
    private SoItemService soItemService;

    @Autowired
    private StockService stockService;

    public void addUser(ScmUser scmUser, String powers){
        //存放当前日期
        String str = new SimpleDateFormat("yyyy-MM--dd").format (new Date());
        scmUser.setCreateDate(str);
        String[] power = powers.split(",");
        System.out.println(scmUser);
        // 获取权限的集合
        List<UserModel> list = new ArrayList<>();
        for (String s:power){
            list.add(new UserModel(scmUser.getAccount(),s));
        }
        scmService.addUser(scmUser);
        // 添加权限
        for (UserModel userModel: list){
            userModelService.addModel(userModel);
        }

    }
    public void deleteUser(String account){
        userModelService.deleteModel(account);
        scmService.deleteUser(account);

    }
    public void updateUser(ScmUser user,String powers){
        //删除对应权限
        String account = scmService.findAccount(user.getAccount2());
        userModelService.deleteModel(account);
        //修改用户
        scmService.updateUser(user);
        //重新添加权限
        String[] split = powers.split(",");
        for (String s :split){
            userModelService.addModel(new UserModel(user.getAccount(),s));
        }

    }

    //采购单添加
    public void addPoMain(PoMain poMain){
        poMain.setStatus("1");
        poMainService.addPoMain(poMain);
        for (PoItem poItem:poMain.getItems()){
            poMainService.addItem(poItem);
            //添加采购在途数
            productService.setPoNum(poItem.getNum(),poItem.getProductCode());
        }

    }
    //采购单修改
    public void updatePoMain(PoMain poMain){
        List<PoItem> poItem1 = poItemService.getPoItem(poMain.getPoID());
        for (PoItem poItem:poItem1){
            String num ="-"+poItem.getNum();
            productService.setPoNum(num,poItem.getProductCode());
        }
        poItemService.deletePoItem(poMain.getPoID());
        poMainService.updatePoMain(poMain);
        for (PoItem poItem:poMain.getItems()){
            productService.setPoNum(poItem.getNum(),poItem.getProductCode());
            poMainService.addItem(poItem);
            //刷新采购在途数

        }
    }

//采购单删除 1-删除明细，2-删除主单，3-刷新采购在途数
    public void deletePoMain(String poID){
        poItemService.deletePoItem(poID);
        poMainService.deletePoMain(poID);
        //查询出主单中包含哪几种商品
        List<String> list = poItemService.getProductCode(poID);
        for (String productCode:list){
            util.flushPoNum(productCode);
        }
    }

    //销售单添加
    public void addSoMain(SoMain soMain){
        soMain.setStatus("1");
        soMainService.addSoMain(soMain);
        util.reSoItem(soMain.getItems());
    }

    //    销售单修改
    public void updateSoMain(SoMain soMain){
        System.out.println(soMain);
        soItemService.deleteSoItem(soMain.getSoID());
        soMainService.updateSoMain(soMain);
        util.reSoItem(soMain.getItems());
    }

    //采购入库
    public void putStorage(String poID){
        List<PoItem> poItem = poItemService.getPoItem(poID);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String format = sdf.format(date);
        for (PoItem poItem1:poItem){
            productService.putStorage(poItem1.getNum(),poItem1.getProductCode());
            stockService.addStock(new Stock(poItem1.getProductCode(),poID,poItem1.getNum(),"1",format,"xuzhihong"));
        }
        poMainService.putStorage(poID);
    }
    //销售出库
    public void outStorage(String soID,String account){
        List<SoItem> list = soItemService.getSoItem(soID);
        String date =new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date());
        for (SoItem soItem:list){
            productService.outStorage(soItem.getNum(),soItem.getProductCode());
            stockService.addStock(new Stock(soItem.getProductCode(),soID,soItem.getNum(),"-1",date,account));
        }
        soMainService.outStorage(soID);
    }

    //损益盘点
    public void addCheckStock(String num,CheckStock checkStock){
        Product product = productService.getProduct(checkStock.getProductCode());
        String num1 = product.getNum();
         int i = Integer.parseInt(num1);
         int i1 = Integer.parseInt(num);
         int m =0;
        if (checkStock.getType().equals("损耗")){
            productService.reduceProduct(num,checkStock.getProductCode());
            m=i-i1;

        }else  if (checkStock.getType().equals("盈余")){
            productService.augmentProduct(num,checkStock.getProductCode());
            m=i+i1;
        }
        checkStock.setOriginNum(String.valueOf(i));
        checkStock.setRealNum(String.valueOf(m));
        checkStock.setStockTime(new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date()));
        stockService.addCheckStock(checkStock);


    }
}
